<?php
    session_start();
    trackAnonymousUserVisit();
	if ($_GET)
	{
		require('config.php');
		$uuid = $_GET['uuid'];
		$stmt = $connection->prepare("select * from user_id_passwd_tobeconfirm where uuid=?");
		$stmt->bind_param("s", $uuid);
		$stmt->execute();
		$stmt->store_result();
		$row_cnt = $stmt->num_rows;
		if ($row_cnt > 0)
		{
			$time = date("Y-m-d H:i:s");
			$stmt->bind_result($user, $pass, $uuid);
			$stmt->fetch();
			$connection->autocommit(false);
			$stmt = $connection->prepare("delete from user_id_passwd_tobeconfirm where id=?");
			$stmt->bind_param("s", $user);
			$stmt->execute();
			$md5 = md5($pass);
			$stmt = $connection->prepare("insert into user_id_passwd (email, md5, cookie) values (?, ?, 1)");
			$stmt->bind_param("ss", $user, $md5);
			$stmt->execute();
			$stmt = $connection->prepare("select id from user_id_passwd where email=?");
			$stmt->bind_param("s", $user);
			$stmt->execute();
			$stmt->store_result();
			$stmt->bind_result($id);
			$stmt->fetch();
			$stmt = $connection->prepare("insert into user_fav_lists (user_id, create_time) values (?, ?)");
			$stmt->bind_param("ss", $id, $time);
			$stmt->execute();
			if(!$connection->errno)
			{  
				$connection->commit();  
			}
			else
			{  
				$connection->rollback();  
			}
			$connection->autocommit(true);
			setcookie("userid", $id); 
			setcookie("usermd5", $md5);
            $_SESSION['login'] = true;
            $_SESSION['gate'] = 1;
            $_SESSION['username'] = $user;
            trackRegistedUserVisit($id);
			header("location: home.php");
		}
        else
        {
            $_SESSION['gate'] = 1;
            $_SESSION['login'] = false;
		    header("location: anonymous.php");
        }
	}
	else
	{
        $_SESSION['gate'] = 1;
        $_SESSION['login'] = false;
		header("location: anonymous.php");
	}
	
    function trackAnonymousUserVisit()
    {
        $ip = getuserip();
	    $time = date("y-m-d h:i:s");
        $browser = $_SERVER["HTTP_USER_AGENT"];
		require('config.php');
        $stmt = $connection->prepare('insert into anonymous_user_visit (ip, time, browser) values (?, ?, ?)');
        $stmt->bind_param("sss", $ip, $time, $browser);
        $stmt->execute();
    }

    function getUserIP()
    {
        if(!empty($_SERVER["HTTP_CLIENT_IP"]))  
            $cip = $_SERVER["HTTP_CLIENT_IP"];  
        else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))  
            $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];  
        else if(!empty($_SERVER["REMOTE_ADDR"]))  
            $cip = $_SERVER["REMOTE_ADDR"];  
        else  
            $cip = "无法获取！";  
        return $cip;  
    }
    function trackRegistedUserVisit($user_id)
    {
	    $time = date("y-m-d h:i:s");
        $browser = $_SERVER["HTTP_USER_AGENT"];
		require('config.php');
        $stmt = $connection->prepare('insert into registed_user_visit (user_id, time, browser) values (?, ?, ?)');
        $stmt->bind_param("sss", $user_id, $time, $browser);
        $stmt->execute();
    }
?>
